package com.ruoyi.order.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.order.domain.Dish;
import com.ruoyi.order.domain.SetMeal;
import com.ruoyi.order.exception.OrderException;
import com.ruoyi.order.service.IDishService;
import com.ruoyi.order.service.ISetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.order.mapper.CategoryMapper;
import com.ruoyi.order.domain.Category;
import com.ruoyi.order.service.ICategoryService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 菜品及套餐分类Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-19
 */
@Service
public class CategoryServiceImpl implements ICategoryService
{
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private IDishService dishService;
    @Autowired
    private ISetMealService setMealService;

    /**
     * 查询菜品及套餐分类
     * 
     * @param categoryId 菜品及套餐分类主键
     * @return 菜品及套餐分类
     */
    @Override
    public Category selectCategoryByCategoryId(Long categoryId)
    {
        return categoryMapper.selectCategoryByCategoryId(categoryId);
    }

    /**
     * 查询菜品及套餐分类列表
     * 
     * @param category 菜品及套餐分类
     * @return 菜品及套餐分类
     */
    @Override
    public List<Category> selectCategoryList(Category category)
    {
        return categoryMapper.selectCategoryList(category);
    }

    /**
     * 新增菜品及套餐分类
     * 
     * @param category 菜品及套餐分类
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCategory(Category category)
    {
        category.setCreateTime(DateUtils.getNowDate());
        return categoryMapper.insertCategory(category);
    }

    /**
     * 修改菜品及套餐分类
     * 
     * @param category 菜品及套餐分类
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCategory(Category category)
    {
        category.setUpdateTime(DateUtils.getNowDate());
        return categoryMapper.updateCategory(category);
    }

    /**
     * 批量删除菜品及套餐分类
     * 
     * @param categoryIds 需要删除的菜品及套餐分类主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteCategoryByCategoryIds(Long[] categoryIds)
    {
        for (Long categoryId : categoryIds) {
            List<Dish> dishList = dishService.selectDishList(Dish.builder().categoryId(categoryId).build());
            List<SetMeal> setMealList = setMealService.selectSetMealList(SetMeal.builder().categoryId(categoryId).build());
            if(dishList.size()>0||setMealList.size()>0){
                throw new OrderException(null,categoryIds,"删除失败，该分类下包含子项");
            }
        }
        return categoryMapper.deleteCategoryByCategoryIds(categoryIds);
    }

    /**
     * 删除菜品及套餐分类信息
     * 
     * @param categoryId 菜品及套餐分类主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteCategoryByCategoryId(Long categoryId)
    {
        List<Dish> dishList = dishService.selectDishList(Dish.builder().categoryId(categoryId).build());
        List<SetMeal> setMealList = setMealService.selectSetMealList(SetMeal.builder().categoryId(categoryId).build());
        if(dishList.size()>0||setMealList.size()>0){
            throw new OrderException(null,new Object[]{categoryId},"删除失败，该分类下包含子项");
        }
        return categoryMapper.deleteCategoryByCategoryId(categoryId);
    }
}
